package cn.hlife.com.hlfhaemocyte.systemsetting.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.hlife.com.hlfhaemocyte.MainActivity;
import cn.hlife.com.hlfhaemocyte.R;
import cn.hlife.com.hlfhaemocyte.utils.FragmentUtils;
import cn.hlife.com.hlfhaemocyte.utils.SharedPreferencesUtils;

/**
 * Created by Administrator on 2017/7/29.
 */

public class ReportReferenceSetting extends Fragment implements View.OnClickListener {

    @BindView(R.id.WBC)
    EditText WBC;
    @BindView(R.id.LYM)
    EditText LYM;
    @BindView(R.id.MON)
    EditText MON;
    @BindView(R.id.GRA)
    EditText GRA;
    @BindView(R.id.LYM_P)
    EditText LYMP;
    @BindView(R.id.MON_P)
    EditText MONP;
    @BindView(R.id.GRA_P)
    EditText GRAP;
    @BindView(R.id.HGB)
    EditText HGB;
    @BindView(R.id.MCH)
    EditText MCH;
    @BindView(R.id.MCHC)
    EditText MCHC;
    @BindView(R.id.RBC)
    EditText RBC;
    @BindView(R.id.MCV)
    EditText MCV;
    @BindView(R.id.HCT)
    EditText HCT;
    @BindView(R.id.PLT)
    EditText PLT;
    @BindView(R.id.PLT_Y)
    EditText PLTY;
    @BindView(R.id.MPV)
    EditText MPV;
    @BindView(R.id.PDWSD)
    EditText PDWSD;
    @BindView(R.id.PDWCV)
    EditText PDWCV;
    @BindView(R.id.PDW)
    EditText PDW;
    @BindView(R.id.P_LCR)
    EditText PLCR;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.report_reference_setting, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ((MainActivity) getActivity()).rightText.setVisibility(View.VISIBLE);
        ((MainActivity) getActivity()).middleText.setText("参考值设置");
        ((MainActivity) getActivity()).rightText.setText("保存");
        ((MainActivity) getActivity()).rightText.setOnClickListener(this);
        ((MainActivity) getActivity()).returnImageView.setVisibility(View.VISIBLE);
        ((MainActivity) getActivity()).returnImageView.setOnClickListener(this);
        initView();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            ((MainActivity) getActivity()).rightText.setVisibility(View.VISIBLE);
            ((MainActivity) getActivity()).rightText.setVisibility(View.GONE);
            ((MainActivity) getActivity()).middleText.setText("参考值设置");
            ((MainActivity) getActivity()).returnImageView.setVisibility(View.VISIBLE);
            ((MainActivity) getActivity()).returnImageView.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.return_image:
                ((MainActivity) getActivity()).currentFragment = FragmentUtils.switchFragment
                        (getFragmentManager(), R.id.fragment_content, ((MainActivity) getActivity()).currentFragment, SettingFragment.class, null);
                break;
            case R.id.left_text:
                save();
                break;

        }
    }
    public  void  initView(){
       String  jsonStr= SharedPreferencesUtils.getInstance().getReportRefernce(getContext());
        if(!TextUtils.isEmpty(jsonStr)){
            JSONObject  jsonObject = null;
            try {
                jsonObject = new JSONObject(jsonStr);
                WBC.setText(jsonObject.getString("WBC"));
                LYM.setText(jsonObject.getString("LYM"));
                MON.setText(jsonObject.getString("MON"));
                GRA.setText(jsonObject.getString("GRA"));
                LYMP.setText(jsonObject.getString("LYMP"));
                MONP.setText(jsonObject.getString("MONP"));
                GRAP.setText(jsonObject.getString("GRAP"));
                HGB.setText(jsonObject.getString("HGB"));
                MCH.setText(jsonObject.getString("MCH"));
                MCHC.setText(jsonObject.getString("MCHC"));
                RBC.setText(jsonObject.getString("RBC"));
                HCT.setText(jsonObject.getString("HCT"));
                PLT.setText(jsonObject.getString("PLT"));
                MCV.setText(jsonObject.getString("MCV"));
                PLTY.setText(jsonObject.getString("PLTY"));
                MPV.setText(jsonObject.getString("MPV"));
                PDWSD.setText(jsonObject.getString("PDWSD"));
                PDWCV.setText(jsonObject.getString("PDWCV"));
                PDW.setText(jsonObject.getString("PDW"));
                PLCR.setText(jsonObject.getString("PLCR"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    public  void  save(){
        JSONObject  jsonObject=new JSONObject();
        try {
            jsonObject.put("WBC",WBC.getText().toString());
            jsonObject.put("LYM",LYM.getText().toString());
            jsonObject.put("MON",MON.getText().toString());
            jsonObject.put("GRA",GRA.getText().toString());
            jsonObject.put("LYMP",LYMP.getText().toString());
            jsonObject.put("MONP",MONP.getText().toString());
            jsonObject.put("GRAP",GRAP.getText().toString());
            jsonObject.put("HGB",HGB.getText().toString());
            jsonObject.put("MCH",MCH.getText().toString());
            jsonObject.put("MCHC",MCHC.getText().toString());
            jsonObject.put("RBC",RBC.getText().toString());
            jsonObject.put("HCT",HCT.getText().toString());
            jsonObject.put("PLT",PLT.getText().toString());
            jsonObject.put("MCV",MCV.getText().toString());
            jsonObject.put("PLTY",PLTY.getText().toString());
            jsonObject.put("MPV",MPV.getText().toString());
            jsonObject.put("PDWSD",PDWSD.getText().toString());
            jsonObject.put("PDWCV",PDWCV.getText().toString());
            jsonObject.put("PDW",PDW.getText().toString());
            jsonObject.put("PLCR",PLCR.getText().toString());
            SharedPreferencesUtils.getInstance().saveReportRefernce(getContext(),jsonObject.toString());
            Toast.makeText(getContext(),"数据已保存",Toast.LENGTH_SHORT).show();
            ((MainActivity) getActivity()).currentFragment = FragmentUtils.switchFragment
                    (getFragmentManager(), R.id.fragment_content, ((MainActivity) getActivity()).currentFragment, SettingFragment.class, null);
        } catch (JSONException e){
            e.printStackTrace();
        }
    }
}
